package com.example.demo02.enums;

import com.example.demo02.constants.Constants;
import com.example.demo02.exception.InvalidParameterException;
import com.example.demo02.exception.LoginInvalidException;
import com.example.demo02.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.transaction.TransactionException;

import java.sql.SQLException;
import java.util.function.Function;

@Getter
@AllArgsConstructor
public enum ExceptionEnum {
    LOGIN_VALID_EXCEPTION(Constants.NO_LOGIN, LoginInvalidException.class,Throwable::getMessage,false),
    INVALID_PARAMETER_EXCEPTION(InvalidParameterException.class,Throwable::getMessage,false),
    SERVICE_NORMAL_EXCEPTION(ServiceException.class,Throwable::getMessage),
    CANNOT_CREATE_TRANSACTION_EXCEPTION(CannotCreateTransactionException.class,t->"数据库无法连接,请稍后重试"),
    TRANSACTION_EXCEPTION(TransactionException.class,t->"数据库异常"),
    MY_BATIS_SYSTEM_EXCEPTION(MyBatisSystemException.class,t->"服务器sql执行异常"),
    SQL_EXCEPTION(SQLException.class,t->"服务器sql异常"),
    RUNTIME_EXCEPTION(RuntimeException.class,t->"服务器运行异常"),
    DEFAULT_EXCEPTION(Exception.class,t->"服务器异常，请稍后重试");
    private final Integer result;
    private final Class rootCause;
    private final Function<Throwable,String> function;
    private final boolean needPrintStackTraceOperate;

    ExceptionEnum(Class rootCause, Function<Throwable, String> function) {
        this(rootCause,function,true);
    }

    ExceptionEnum(Class rootCause, Function<Throwable, String> function, boolean needPrintStackTraceOperate) {
        this(Constants.FAIL,rootCause,function,needPrintStackTraceOperate);
    }
}
